﻿using BaseFramework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteFramework.Contract
{
    public interface IAbsContract<T>
        where T : PersistentEntity<T>
    {
        /// <summary> 统计
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        int Count(BaseFramework.IExpression expression = null);
        /// <summary> 对象是否存在
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        bool Exist(object primaryKey);
        /// <summary> 对象是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        bool Exist(BaseFramework.IExpression expression);
        /// <summary> 获取一个对象
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        T Get(object primaryKey);
        /// <summary> 获取全部对象
        /// </summary>
        /// <returns></returns>
        IEnumerable<T> GetAll();


        /// <summary> 获取第一个对象
        /// </summary>
        /// <param name="orderParam"></param>
        /// <returns></returns>
        T FindFirst(OrderParam orderParam);
        /// <summary> 获取第一个对象
        /// </summary>
        /// <param name="searchParam"></param>
        /// <returns></returns>
        T FindFirst(SearchParam searchParam);
        /// <summary> 获取第一个对象
        /// </summary>
        /// <param name="searchParam"></param>
        /// <param name="orderParam"></param>
        /// <returns></returns>
        T FindFirst(SearchParam searchParam, OrderParam orderParam);


        /// <summary> 获取对象集合
        /// </summary>
        /// <param name="searchParam"></param>
        /// <returns></returns>
        T[] GetList(SearchParam searchParam);
        /// <summary> 获取对象集合
        /// </summary>
        /// <param name="searchParam"></param>
        /// <param name="orderParam"></param>
        /// <returns></returns>
        T[] GetList(SearchParam searchParam, OrderParam orderParam);
        /// <summary> 获取对象集合
        /// </summary>
        /// <param name="searchParam"></param>
        /// <param name="pageParam"></param>
        /// <returns></returns>
        T[] GetList(SearchParam searchParam, ref PageParam pageParam);
        /// <summary> 获取对象集合
        /// </summary>
        /// <param name="pageParam"></param>
        /// <returns></returns>
        T[] GetList(ref PageParam pageParam);
        /// <summary> 获取对象集合
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="asc"></param>
        /// <returns></returns>
        T[] GetList(IExpression expression, int currentPage = 1, int pageSize = int.MaxValue, string orderBy = null, bool asc = true);


        /// <summary> GetTable
        /// </summary>
        /// <param name="searchParam"></param>
        /// <returns></returns>
        DataTable GetTable(SearchParam searchParam);
        /// <summary> GetTable
        /// </summary>
        /// <param name="searchParam"></param>
        /// <param name="orderParam"></param>
        /// <returns></returns>
        DataTable GetTable(SearchParam searchParam, OrderParam orderParam);
        /// <summary> GetTable
        /// </summary>
        /// <param name="searchParam"></param>
        /// <param name="pageParam"></param>
        /// <returns></returns>
        DataTable GetTable(SearchParam searchParam, ref PageParam pageParam);
        /// <summary> GetTable
        /// </summary>
        /// <param name="pageParam"></param>
        /// <returns></returns>
        DataTable GetTable(ref PageParam pageParam);
        /// <summary> GetTable
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="asc"></param>
        /// <returns></returns>
        DataTable GetTable(IExpression expression, int currentPage = 1, int pageSize = int.MaxValue, string orderBy = null, bool asc = true);


        /// <summary> 新建对象。
        /// <para>新建失败时，将抛出异常</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        bool Create(T model);
        /// <summary> 新建对象。
        /// </summary>
        /// <param name="model"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        bool Create(T model, out string error);


        /// <summary> 修改对象。
        /// <para>修改失败时，将抛出异常</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        bool Update(T model);
        /// <summary> 修改对象。
        /// </summary>
        /// <param name="model"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        bool Update(T model, out string error);


        /// <summary> 删除对象。
        /// <para>删除失败时，将抛出异常</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        bool Delete(T model);
        /// <summary> 删除对象。
        /// </summary>
        /// <param name="model"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        bool Delete(T model, out string error);


        /// <summary> 删除对象集合
        /// <para>全部失败时，抛出异常</para>
        /// </summary>
        /// <param name="expression"></param>
        /// <returns>返回删除成功的行数。全部失败时，抛出异常</returns>
        int DeleteByExpression(BaseFramework.IExpression expression = null);
        /// <summary> 删除对象集合
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="error"></param>
        /// <returns>返回删除成功的行数。</returns>
        int DeleteByExpression(BaseFramework.IExpression expression, out string error);


        /// <summary> 删除对象
        /// <para>失败时，抛出异常</para>
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns>返回true删除成功；false删除失败。失败时，将抛出异常</returns>
        bool DeleteByPrimaryKey(object primaryKey);
        /// <summary> 删除对象
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <param name="error"></param>
        /// <returns>返回true删除成功；false删除失败。</returns>
        bool DeleteByPrimaryKey(object primaryKey, out string error);


        /// <summary> 删除对象集合
        /// <para>全部失败时，抛出异常</para>
        /// </summary>
        /// <param name="primaryKeys"></param>
        /// <returns>返回删除成功的行数。全部失败时，抛出异常</returns>
        int DeleteByPrimaryKeys(object[] primaryKeys);
        /// <summary> 删除对象集合
        /// </summary>
        /// <param name="primaryKeys"></param>
        /// <param name="error"></param>
        /// <returns>返回删除成功的行数。</returns>
        int DeleteByPrimaryKeys(object[] primaryKeys, out string error);
    }
}